package com.huahai.controller;

import com.huahai.common.BaseResponse;
import com.huahai.common.ErrorCode;
import com.huahai.common.ResultUtils;
import com.huahai.exception.BusinessException;
import com.huahai.pojo.entity.User;
import com.huahai.pojo.vo.UserFollowVO;
import com.huahai.service.UserFollowService;
import com.huahai.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 29095
 * <p>
 * 用户好友关系管理层
 */
@RestController
@RequestMapping("/follow")
@Slf4j
public class UserFollowController {

    @Resource
    private UserFollowService userFollowService;

    @Resource
    private UserService userService;

    /**
     * 关注用户
     *
     * @param followId
     * @param request
     * @return
     */
    @PutMapping("/add/{id}")
    public BaseResponse<Boolean> addFollow(@PathVariable(value = "id") Long followId, HttpServletRequest request) {
        if (followId == null || followId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 添加用户好友关联信息
        User loginUser = userService.getCurrentUser(request);
        Long userId = loginUser.getId();
        Boolean result = userFollowService.addFollow(userId, followId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "关注失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 取消关注用户（删除用户）
     *
     * @param followId
     * @param request
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Boolean> deleteFollow(@PathVariable(value = "id") Long followId, HttpServletRequest request) {
        if (followId == null || followId < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取当前登录用户
        User loginUser = userService.getCurrentUser(request);
        Long userId = loginUser.getId();
        boolean result = userFollowService.removeFollow(userId, followId);
        return ResultUtils.success(true);
    }

    /**
     * 获取用户关注列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<UserFollowVO> listFollow(HttpServletRequest request) {
        // 获取当前用户
        User loginUser = userService.getCurrentUser(request);
        // 获取用户关注列表
        UserFollowVO userFollowVO = userFollowService.listUserFollows(loginUser);
        if (userFollowVO == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "获取用户关注列表失败");
        }
        return ResultUtils.success(userFollowVO);
    }

    /**
     * 查询当前用户是否关注了目标用户
     *
     * @param followId 目标用户ID
     * @param request
     * @return true-已关注，false-未关注
     */
    @GetMapping("/status/{id}")
    public BaseResponse<Boolean> checkFollowStatus(
            @PathVariable(value = "id") Long followId,
            HttpServletRequest request
    ) {
        if (followId == null || followId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取当前登录用户
        User loginUser = userService.getCurrentUser(request);
        Long userId = loginUser.getId();
        boolean isFollowed = userFollowService.isUserFollowed(userId, followId);

        return ResultUtils.success(isFollowed);
    }

}
